<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/jsp/include/taglib.jsp"%>
<%@ include file="/jsp/include/headlist.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="${ctxStatic}/css/zTreeStyle/demo.css"
	type="text/css">
<link rel="stylesheet" href="${ctxStatic}/css/zTreeStyle/zTreeStyle.css"
	type="text/css">
<script type="text/javascript"
	src="${ctxStatic}/jquery/zTree/jquery.ztree.core.js"></script>
<script type="text/javascript"
	src="${ctxStatic}/jquery/zTree/jquery.ztree.excheck.js"></script>
<script type="text/javascript"
	src="${ctxStatic}/jquery/zTree/jquery.ztree.exedit.js"></script>

	<SCRIPT type="text/javascript">
		var setting = {
			async : {
				enable : false,
				url : getAsyncUrl,
				type : 'get',
				dataType : "json",//默认text 
				dataFilter : filter
			//异步返回后经过Filter 
			},
			view: {
				selectedMulti: false,
				dblClickExpand : false,
				showLine: false
			},
			edit: {
				enable: false,
				editNameSelectAll: false,
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick : onClick,
			}
		};
		
		//treeId是treeDemo 
		function filter(treeId, parentNode, childNodes) {
			if (!childNodes || !childNodes.data)
				return null;

			var ztree = getZtreeJSON(childNodes);
			return ztree;
		}
		
		function getZtreeJSON(result){
			
			var zTreeData=[];
			
			if(result.data!=null){
				$.each(result.data, function(i, n) {
					var paths=n.node.path.split('/');
					/* zTreeData[i] = {
						id : n.nobj+"_"+n.nid,
						pId :n.pobj+"_"+n.pid,
						name : n.node.name,
						isParent : isParent('${ctx}/api/resource/' +n.nid+ '/child?ticket=' + ticket+ '&objFilter=nobj,resource&lv=1'),
						//isParent : true,
						url:paths[1]!=null?'${ctx}/jsp/catalog/catalogList.jsp?type='+paths[1]:'',
						nobj:n.nobj,
						pobj:n.pobj,
						nid:n.nid,
						pid:n.pid,
// 						target:"_blank"
					}; */
				});
			}		
			return zTreeData;
		}
		
	
		function getAsyncUrl(treeId, treeNode) {
			if (treeNode == null)
				return null;
			return "${ctx}/api/resource/" + treeNode.nid + "/child?ticket=" + ticket
					+ "&lv=" + lv;
		}

		
		function selectAll() {
			var zTree = $.fn.zTree.getZTreeObj("resourceTree");
			zTree.setting.edit.editNameSelectAll =  $("#selectAll").attr("checked");
		}
		
		$(document).ready(function(){
			$.fn.zTree.init($("#resourceTree"), setting, null);
			$("#selectAll").bind("click", selectAll);
		});
	
		function onClick(event, treeId, treeNode, clickFlag){
			// var jsonStr = JSON.stringify(treeNode);
			 if(treeNode.level>0){
				 if(parent.onCatalogSelected){
					 parent.onCatalogSelected(treeNode);
				 }
			 }else{
				 var zTree = $.fn.zTree.getZTreeObj("resourceTree");
				 zTree.expandNode(treeNode);
			 }
			 
		}
		function beforeClick(treeId, treeNode, clickFlag){
			if(treeNode.level>0)
				return true;	
			else
				return false;
		}
		
		
		
		
		var Local={
				_pId:2,
				_pType:'privilege',
				_ticket:'1_1',
				_lv:2
		};
		//加载父节点
		//privilege/2/child?ticket=1_1
		$(document).ready(function() {
			$.jBox.tip("加载中...", 'loading');
			$.get('${ctx}/api/privilege/'+Local._pId+'/child?ticket='+Local._ticket+"&r="+Math.random(), {'lv':Local._lv}, function(result) {
				//var zTreeData = getZtreeJSON(result);
			
				if(!result.info.success){
					$.jBox.closeTip();
					return ;
				}else if(result.data==null){
					$.jBox.closeTip();
					return;
				}
				
				 var zTreeData =new Array();
				//得到根节点
				$.each(result.data,function(i,n){
					if(n.pobj==Local._pType){//根节点
						zTreeData.push({
							id : n.nobj+"_"+n.nid,
							pId : n.pobj+"_"+n.pid,
							name : n.node.name,
							isParent : true,
							nobj:n.nobj,
							pobj:n.pobj,
							nid:n.nid,
							pid:n.pid,
							tId:"resourceTree_"+n.nobj+"_"+n.nid,
						});		
					}else{//子节点
						
						//获取type和resourceType
						var paths=n.node.path.split('/');
					
						if(n.node.path.indexOf('cardMain.jsp')==-1 && n.node.path.indexOf('summaryMain.jsp')==-1)
							return;
						var type=paths[1];
						var resourceType=paths[2].substring(paths[2].indexOf('=')+1,paths[2].length);
						//var isP=isParent(resourceType,type);
						var path='jsp/catalog/catalogManage.jsp?resourceType='+resourceType+'&type='+type+"&isParent="+false;
						zTreeData.push({
							id : n.nobj+"_"+n.nid,
							pId : n.pobj+"_"+n.pid,
							name : n.node.name,
							isParent : false,
							nobj:n.nobj,
							pobj:n.pobj,
							nid:n.nid,
							pid:n.pid,
							path:path,
							tId:"resourceTree_"+n.nobj+"_"+n.nid,
						});		
					}
				});
				$.jBox.closeTip();
				$.fn.zTree.init($("#resourceTree"), setting, zTreeData);
			}, 'json').error(function(){$.jBox.closeTip();});
		});
		
			
			function isParent(resourceType,type){
				
				var flag=false;
				$.ajax({
					url:'${ctx}/catalog/1/child?ticket='+Local._ticket+'&lv=1&r='+Math.random(),
					dataType:'json',
					type:'get',
					async:false,
					success:function(result){

						if(!result.info.success)
							flag=false;
				
						if(result.data!=null){
							var n=result.data;
							var count=0;
							for (var i = 0; i < n.length; i++) {
								if(n[i].node.type==type &&  n[i].node.resourceType==resourceType){
									count++;
									break;									
								}
							}
							if(count>0)
								flag= true;
							else 
								flag= false;
						}else
							return false;
					}
				}).error(function(){return flag=false;});
				return flag;
		}
	</SCRIPT>
	<style type="text/css">
	body{
	background: #efefef;
	}
.ztree li span.button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle}
	
	</style>
</HEAD>

<BODY>
<div class="left">
		<ul id="resourceTree" class="ztree"></ul>
</div>
</BODY>
</HTML>